{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Count Elements With Strictly Smaller and Greater Elements "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: countElements"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #元素计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组 <code>nums</code> ，统计并返回在 <code>nums</code> 中同时至少具有一个严格较小元素和一个严格较大元素的元素数目。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [11,7,2,15]\n",
    "<strong>输出：</strong>2\n",
    "<strong>解释：</strong>元素 7 ：严格较小元素是元素 2 ，严格较大元素是元素 11 。\n",
    "元素 11 ：严格较小元素是元素 7 ，严格较大元素是元素 15 。\n",
    "总计有 2 个元素都满足在 nums 中同时存在一个严格较小元素和一个严格较大元素。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [-3,3,3,90]\n",
    "<strong>输出：</strong>2\n",
    "<strong>解释：</strong>元素 3 ：严格较小元素是元素 -3 ，严格较大元素是元素 90 。\n",
    "由于有两个元素的值为 3 ，总计有 2 个元素都满足在 nums 中同时存在一个严格较小元素和一个严格较大元素。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 100</code></li>\n",
    "\t<li><code>-10<sup>5</sup> &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [count-elements-with-strictly-smaller-and-greater-elements](https://leetcode.cn/problems/count-elements-with-strictly-smaller-and-greater-elements/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [count-elements-with-strictly-smaller-and-greater-elements](https://leetcode.cn/problems/count-elements-with-strictly-smaller-and-greater-elements/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[11,7,2,15]', '[-3,3,3,90]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        l = min(nums)\n",
    "        r = max(nums)\n",
    "        ans = 0\n",
    "        for i in nums:\n",
    "            if i != l and i != r:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        a, b = max(nums), min(nums)\n",
    "        m, n = nums.count(a), nums.count(b)\n",
    "        if a == b:\n",
    "            return 0\n",
    "        return len(nums) - (m + n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        a = max(nums)\n",
    "        b = min(nums)\n",
    "        res = 0 \n",
    "        for i in nums:\n",
    "            if b < i < a:\n",
    "                res+=1\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        f = Counter(nums)\n",
    "        minnum, maxnum = min(f), max(f)\n",
    "        return sum(f[num] for num in f if minnum < num < maxnum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        smallest = min(nums)\n",
    "        largest = max(nums)\n",
    "        res = 0\n",
    "        for num in nums:\n",
    "            if smallest < num < largest:\n",
    "                res += 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        minx, maxx = min(nums), max(nums)\n",
    "        res = 0\n",
    "        for i in nums:\n",
    "            res += (i != minx and i != maxx)\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        total = 0 # 用来表示同时具有严格较小和严格较大元素的元素数目\n",
    "        my_min = min(nums)\n",
    "        my_max = max(nums)\n",
    "        for num in nums:\n",
    "            # if nums[i-1]<nums[i]<nums[i+1]:\n",
    "            #     total+=1\n",
    "            # elif nums[i-1]<nums[i]<my_max:\n",
    "            #     total+=1\n",
    "            # elif my_min<nums[i]<nums[i+1]:\n",
    "            #     total+=1\n",
    "            if my_min<num<my_max:\n",
    "                total+=1\n",
    "            \n",
    "        \n",
    "        return total\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "\n",
    "        return sum(min(nums)< num < max(nums) for num in nums)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        dic = Counter(nums)\n",
    "        a, b = min(nums), max(nums)\n",
    "        if a == b:\n",
    "            return 0\n",
    "        return len(nums) - (dic[a] + dic[b])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        a, b = min(nums), max(nums)\n",
    "\n",
    "        return sum(num > a and num < b for num in nums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        if nums[0] == nums[-1]:\n",
    "            return 0\n",
    "        i, j = 0, n - 1\n",
    "        while nums[i] == nums[0]:\n",
    "            i += 1\n",
    "        while nums[j] == nums[-1]:\n",
    "            j -= 1\n",
    "        return j - i + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        min_val = 100000\n",
    "        max_val = -100000\n",
    "        min_count = 0\n",
    "        max_count = 0\n",
    "        for num in nums:\n",
    "            if num < min_val:\n",
    "                min_val = num\n",
    "                min_count = 1\n",
    "            elif num == min_val:\n",
    "                min_count += 1\n",
    "            if num > max_val:\n",
    "                max_val = num\n",
    "                max_count = 1\n",
    "            elif num == max_val:\n",
    "                max_count += 1\n",
    "        ans = len(nums) - min_count - max_count\n",
    "        return ans if ans > 0 else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "\n",
    "        new_nums =[]\n",
    "        max_num = max(nums)\n",
    "        min_num = min(nums)\n",
    "        count = 0\n",
    "        for num in nums:\n",
    "            if num not in new_nums:\n",
    "                new_nums.append(num)\n",
    "            elif num==max_num or min_num==num:\n",
    "                new_nums.append(num)\n",
    "                count+=1\n",
    "        result = len(new_nums)-2+(len(nums)-len(new_nums))-count\n",
    "        if result <0:\n",
    "            result =0\n",
    "        return result\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        l, u = min(nums), max(nums)\n",
    "        count = 0\n",
    "\n",
    "        for num in nums:\n",
    "            if l < num < u:\n",
    "                count += 1\n",
    "\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        count = 0\n",
    "        left,right = nums[0],nums[-1]\n",
    "        for i in range(n):\n",
    "            if nums[i]==left or nums[i]==right:\n",
    "                continue\n",
    "            else:\n",
    "                count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        dic = Counter(nums)\n",
    "        a, b = min(nums), max(nums)\n",
    "        if a == b:\n",
    "            return 0\n",
    "        return len(nums) - (dic[a] + dic[b])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: list[int]) -> int:\n",
    "        nums.sort()\n",
    "        count = 0\n",
    "        for num in nums:\n",
    "            if num != nums[0] and num != nums[-1]:\n",
    "                count += 1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        nums.sort()\n",
    "        ans = 0\n",
    "        for i in range(1,n-1):\n",
    "            if nums[0] < nums[i] < nums[n-1]:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        minimum, maximum = min(nums), max(nums)\n",
    "        ans = 0\n",
    "        for num in nums:\n",
    "            if minimum < num < maximum:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        mi=min(nums)\n",
    "        ma=max(nums)\n",
    "        return sum(mi< num < ma for num in nums)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        mmax = max(nums)\n",
    "        mmin = min(nums)\n",
    "        \n",
    "        res = 0\n",
    "        for i in nums:\n",
    "            if mmin < i < mmax:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        return sum(i != nums[0] and i != nums[-1] for i in nums[1:-1])\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        total = 0\n",
    "\n",
    "        if len(nums) < 3:\n",
    "            pass\n",
    "        else:\n",
    "            for i in range(1,len(nums)-1):\n",
    "                if max(nums) > nums[i] > min(nums):\n",
    "                    total += 1\n",
    "            else:\n",
    "                pass\n",
    "\n",
    "        return total"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        dic=Counter(nums)\n",
    "        list1=list(dic.values())\n",
    "        count=0\n",
    "        n=len(dic)\n",
    "        if(n<=2):\n",
    "            return 0\n",
    "        for i in range(1,n-1):\n",
    "            count+=int(list1[i])\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        a = sorted(set(nums))\n",
    "        n = len(a)\n",
    "        if n<=2:\n",
    "            return 0\n",
    "        else:\n",
    "            a.remove(a[0])\n",
    "            a.remove(a[-1])\n",
    "            res = 0\n",
    "            for i in range(n-2):\n",
    "                res += nums.count(a[i])\n",
    "            return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        n0,n1 = min(nums),max(nums)\n",
    "        return sum(1 for x in nums if x > n0 and x < n1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        ret = 0\n",
    "        c = Counter(nums)\n",
    "        res = sorted(set(nums))\n",
    "        if len(res) <= 2:\n",
    "            return 0\n",
    "        for i in res[1: -1]:\n",
    "            ret += c.get(i)\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        min_num=min(nums)\n",
    "        max_num=max(nums)\n",
    "        count=0\n",
    "        for num in nums:\n",
    "            if num !=min_num and num !=max_num:\n",
    "                count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        count=0\n",
    "        for i in nums:\n",
    "            if i >min(nums) and i<max(nums):\n",
    "                count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        count = 0\n",
    "        for i in nums:\n",
    "            if i!=nums[0] and i!=nums[-1]:\n",
    "                count+=1\n",
    "\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        length = len(nums)\n",
    "        left, right = 0, length - 1\n",
    "        begin, end = nums[left], nums[right]\n",
    "        if nums[left] == nums[right]:\n",
    "            return 0\n",
    "        while begin == nums[left]:\n",
    "            left += 1\n",
    "        if nums[left] == nums[right]:\n",
    "            return 0\n",
    "        while end == nums[right]:\n",
    "            right -= 1\n",
    "        return right - left + 1        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        mi = min(nums)\n",
    "        ma = max(nums)\n",
    "        for num in nums:\n",
    "            if num > mi and num < ma:\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        count = 0\n",
    "        nums_len = len(nums)\n",
    "        for i in range(1, nums_len - 1):\n",
    "            if nums[0] < nums[i] < nums[-1]:\n",
    "                count += 1\n",
    "\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        n = len(nums)\n",
    "        count = 0\n",
    "        flag=0\n",
    "        for i in range(1,n-1,1):\n",
    "            for j in range(0,n,1):\n",
    "                if nums[i]>nums[j]:\n",
    "                    flag=1\n",
    "                if nums[i]<nums[j] and flag==1:\n",
    "                    count+=1\n",
    "                    flag=0\n",
    "        return count\n",
    "               \n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        C = Counter(nums)\n",
    "        L = list(C.keys())\n",
    "        L.sort()\n",
    "        res = 0\n",
    "        for i in range(1,len(L)-1):\n",
    "            res += C[L[i]]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        minnum = nums[0]\n",
    "        maxnum = nums[len(nums) - 1]\n",
    "        sum = 0\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] != maxnum and nums[i] != minnum:\n",
    "                sum+=1\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        min_num = min(nums)\n",
    "        max_num = max(nums)\n",
    "        c = 0\n",
    "        for n in nums:\n",
    "            if min_num < n < max_num:\n",
    "                c += 1\n",
    "        return c\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        if len(nums)<2:\n",
    "            return 0\n",
    "        count=Counter(nums)\n",
    "        if max(nums)==min(nums):\n",
    "            return 0\n",
    "        return len(nums)-(count[max(nums)]+count[min(nums)])\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "\n",
    "        if len(nums)<3:\n",
    "            return 0\n",
    "\n",
    "        tmax = max(nums)\n",
    "        tmin = min(nums)\n",
    "\n",
    "        count = 0\n",
    "        for i in nums:\n",
    "            if i<tmax and i > tmin:\n",
    "                count += 1\n",
    "\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        n=len(nums)\n",
    "        if n<=2:\n",
    "            return 0\n",
    "        max_val=max(nums)\n",
    "        min_val=min(nums)\n",
    "        cnt=0\n",
    "        for num in nums:\n",
    "            if min_val<num<max_val:\n",
    "                cnt+=1\n",
    "        return cnt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        res = []\n",
    "        ans = 0\n",
    "        nums1 = list(set(nums))\n",
    "        nums1.sort()\n",
    "        nums.sort()\n",
    "        print(nums, nums1)\n",
    "        for i in range(0, len(nums1)-2):\n",
    "            print(nums1[i], nums1[i+1], nums1[i+2])\n",
    "            res.append(nums1[i+1])\n",
    "        print(res)\n",
    "        for i in res:\n",
    "            ans += nums.count(i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        mn, mx = min(nums), max(nums)\n",
    "        return len([x for x in nums if x != mn and x != mx])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        d = collections.Counter(nums)\n",
    "        if len(d.keys()) <= 2:\n",
    "            return 0\n",
    "        min_key = min(d.keys())\n",
    "        del d[min_key]\n",
    "        max_key = max(d.keys())\n",
    "        del d[max_key]\n",
    "        return sum(d.values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        minNum,maxNum = min(nums),max(nums)\n",
    "        return len(list(filter(lambda x:minNum<x<maxNum,nums)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        total = 0\n",
    "\n",
    "        for i in range(1,len(nums)-1):\n",
    "            if max(nums) > nums[i] > min(nums):\n",
    "                total += 1\n",
    "        else:\n",
    "            pass\n",
    "\n",
    "        return total"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        smallest = min(nums)\n",
    "        largest = max(nums)\n",
    "        res = 0\n",
    "        for num in nums:\n",
    "            if smallest < num < largest:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "\n",
    "        return max(0,len(nums)-nums.count(max(nums))-nums.count(min(nums)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        nums=sorted(nums)\n",
    "        n=len(nums)\n",
    "        ans=0\n",
    "        l,r=0,1\n",
    "        for i in range(1,n-1):\n",
    "            if nums[l]<nums[i]:\n",
    "                while r<n and nums[r]<=nums[i]:\n",
    "                    r+=1\n",
    "                if r<n:\n",
    "                    ans+=1\n",
    "        return ans\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        counter = Counter(nums)\n",
    "        nums = list(set(nums))\n",
    "        nums.sort()\n",
    "        nums = [nums[-1]]+nums+[nums[0]]\n",
    "        ans = 0\n",
    "        for i in range(1,len(nums)-1):\n",
    "            if nums[i-1]<nums[i]<nums[i+1] or nums[i-1]>nums[i]>nums[i+1]:\n",
    "                ans += counter[nums[i]]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        length = len(nums)\n",
    "        minnum = nums.count(min(nums))\n",
    "        maxnum = nums.count(max(nums))\n",
    "        if length - maxnum - minnum > 0:\n",
    "            return length - maxnum - minnum\n",
    "        else:\n",
    "            return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        t=len(nums)\n",
    "        if t<=2:\n",
    "            return 0\n",
    "        dct=collections.Counter(nums)\n",
    "        if len(dct)==1:\n",
    "            return 0\n",
    "        key=dct.keys()\n",
    "        m,n=max(key),min(key)\n",
    "        m_1,n_1=dct[m],dct[n]\n",
    "        return t-m_1-n_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        s=0\n",
    "        for i in nums:\n",
    "            if i >nums[0] and i<nums[-1]:\n",
    "                s+=1\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        n=len(nums)\n",
    "        cnt=2\n",
    "        print(nums)\n",
    "        for i in nums[1:]:\n",
    "            if i!=nums[0]:\n",
    "                break\n",
    "            else:\n",
    "                cnt+=1\n",
    "        for i in nums[-2::-1]:\n",
    "            if i!=nums[-1]:\n",
    "                break\n",
    "            else:\n",
    "                cnt+=1\n",
    "        return n-cnt if n-cnt>0 else 0\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        n = len(nums)\n",
    "        nums.sort()\n",
    "        for i in range(n):\n",
    "            if nums[i] == nums[0] or nums[i] == nums[n-1]:\n",
    "                continue\n",
    "            ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        dic = collections.Counter(nums)\n",
    "        return max(len(nums) - dic[max(nums)] - dic[min(nums)],0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        def bubbleSort(arr):\n",
    "            for i in range(len(arr)-1):\n",
    "                for j in range(len(arr)-i-1):\n",
    "                    if arr[j] > arr[j+1]:\n",
    "                        arr[j], arr[j + 1] = arr[j + 1], arr[j]\n",
    "            return (arr)\n",
    "        ans = 0\n",
    "        n = len(nums)\n",
    "        bubbleSort(nums)\n",
    "        for i in range(n):\n",
    "            if nums[i] == nums[0] or nums[i] == nums[n-1]:\n",
    "                continue\n",
    "            ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        nums.sort()\n",
    "        if nums[-1] == nums[0]:\n",
    "            return 0\n",
    "        count = 0\n",
    "        for i in range(1, len(nums) - 1):\n",
    "            if nums[0] < nums[i] < nums[-1]:\n",
    "                count += 1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        length = len(nums)\n",
    "        minnum = nums.count(min(nums))\n",
    "        maxnum = nums.count(max(nums))\n",
    "        if length - maxnum - minnum > 0:\n",
    "            return length - maxnum - minnum\n",
    "        else:\n",
    "            return 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def bubbleSort(self,nums):\n",
    "        n = len(nums)\n",
    "        for i in range(n-1,-1,-1):\n",
    "            for j in range(0,i):\n",
    "                if nums[j] > nums[j+1]:\n",
    "                    nums[j],nums[j+1] = nums[j+1],nums[j]\n",
    "            \n",
    "\n",
    "\n",
    "\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        # self.bubbleSort(nums)\n",
    "        nums.sort()\n",
    "        ans = 0\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            if nums[i] == nums[0] or nums[i] == nums[n-1]:\n",
    "                continue\n",
    "            else:\n",
    "                ans += 1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def countElements(self, nums: List[int]) -> int:\n",
    "#         ans = 0\n",
    "#         mi = min(nums)\n",
    "#         ma = max(nums)\n",
    "#         for num in nums:\n",
    "#             if num > mi and num < ma:\n",
    "#                 ans += 1\n",
    "#         return ans\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "        ans = 0\n",
    "        mi = 10**6\n",
    "        ma = -10**6\n",
    "        cntmi = 1\n",
    "        cntma = 1\n",
    "        lens = 0\n",
    "        for num in nums:\n",
    "            lens += 1\n",
    "            if num < mi:\n",
    "                mi = num\n",
    "                cntmi = 1\n",
    "            elif num == mi:\n",
    "                cntmi += 1\n",
    "            if num > ma:\n",
    "                ma = num\n",
    "                cntma = 1\n",
    "            elif num == ma:\n",
    "                cntma += 1\n",
    "        if mi == ma:\n",
    "            return 0\n",
    "        return lens - cntma - cntmi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countElements(self, nums: List[int]) -> int:\n",
    "       \n",
    "\n",
    "        return len(nums) - nums.count(min(nums)) - nums.count(max(nums)) if len(nums) > 2 and max(nums) != min(nums) else 0\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
